package cn.edu.qlnu.xky.test.dao;

import cn.edu.qlnu.xky.test.common.Page;
import cn.edu.qlnu.xky.test.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : wuweihong
 * @desc : TODO  请填写你的功能描述
 * @date : 2025-06-20
 */


public class ReaderDao {

	BaseDao dao = null;

	public ReaderDao() {
		dao = new BaseDao();
	}

	public int addReader(User reader) {
		if (dao.isClosed()) {
			dao = new BaseDao();
		}
		//将对象中的数据，附加到insert sql中，调用BaseDao的方法
		String sql = "insert into t_user(userName,userPsw,userDisplayName,type,isValid,temp)"
				+ "values('"
				+reader.getUserName()+"','"
				+ "666666" +"','"
				+reader.getUserDisplayName()+"',"
				+ "0" +","
				+ "0" +",'"
				+reader.getTemp()+"')";
		return dao.doAddEditDelete(sql);
	}

	public int editReader(User reader) {
		if (dao.isClosed()) {
			dao = new BaseDao();
		}
		String sql = "update t_user set "
				+ "userName='"+reader.getUserName()+"',"
				+ "userDisplayName='"+reader.getUserDisplayName()+"',"
				+ "temp='"+reader.getTemp()+"' "
				+ "where isValid=0 and type=0 and  id="+reader.getId();
		return dao.doAddEditDelete(sql);
	}

	public int resetPwdReader(int id) {
		String sql = "update t_user set userPsw=666666 where id="+id;
		return dao.doAddEditDelete(sql);
	}

	public int deleteReader(int id) {
		String sql = "update t_user set isValid=1 where id="+id;
		return dao.doAddEditDelete(sql);
	}

	public User searchById(int id) {
		String sql = "select * from t_user where isValid=0 and type=0 and id="+id;
		List<User> readers = this.searchBySql(sql);
		if(readers.size() == 0)
			return new User();
		else
			return readers.get(0);
	}

	private List<User> searchBySql(String sql){
		List<User> readers = new ArrayList<User>();
		List<Map<String,Object>> objs = dao.searchBySql(sql);
		User reader = null;
		for(Map<String,Object> obj : objs) {
			reader = new User();
			reader.setUserDisplayName((String)obj.get("userDisplayName"));
			reader.setId((int)obj.get("id"));
			reader.setTemp((String)obj.get("temp"));
			reader.setUserName((String)obj.get("userName"));

			readers.add(reader);
		}
		return readers;
	}

	public Page<User> searchByNamePage(String name, int pageNo){

		String sqlWhere=" where  isValid=0 and type=0 and  userName like '%"+name+"%'";
		String sql = "select * from t_user "+sqlWhere;
		Page<User> p = new Page<User>();
		p.setPageNo(pageNo);//1
		List<User> readers = new ArrayList<User>();

		List<Map<String,Object>> objs = dao.searchByPage(sql,pageNo);
		User reader = null;
		for(Map<String,Object> obj : objs) {
			reader = new User();
			reader.setUserDisplayName((String)obj.get("userDisplayName"));
			reader.setId((int)obj.get("id"));
			reader.setTemp((String)obj.get("temp"));
			reader.setUserName((String)obj.get("userName"));

			readers.add(reader);
		}
		p.setRecords(readers);//3ok

		String sqlCount = "select count(*) from t_user "+sqlWhere;
		int count = new BaseDao().searchCount(sqlCount);
		p.setTotalCount(count);//2ok
		return p;
	}

	public List<User> searchByName(String name){
		String sql = "select * from t_user where isValid=0 and type=0 and  userName like '%"+name+"%'";
		return this.searchBySql(sql);
	}

	public List<User> searchEqualName(String name){
		String sql = "select * from t_user where isValid=0 and type=0 and  userName='"+name+"'";
		return this.searchBySql(sql);
	}

}
